package io.xxx.mario.task.job;

import io.xxx.mario.task.Task;
import io.xxx.mario.task.TaskMapper;
import io.xxx.mario.task.TaskService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.lang.NonNull;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class OncePushJob extends QuartzJobBean {

    @Resource
    private TaskService taskService;

    @Resource
    private TaskMapper taskMapper;

    @Override
    protected void executeInternal(@NonNull JobExecutionContext context) throws JobExecutionException {
        try {
            JobDataMap dataMap = context.getMergedJobDataMap();
            Task task = taskMapper.selectById(dataMap.getLong("taskId"));
            log.info("任务开始执行[taskId:{},taskName:{}]", task.getId(), task.getName());
            taskService.push(task);
        } catch (Exception e) {
            JobExecutionException jee = new JobExecutionException("任务执行失败", e);
            jee.setRefireImmediately(true);
            throw jee;
        }
    }
}
